from django.db import models
from api.subject_management.models import Subject
from api.user_management.models import User
import django.utils.timezone as timezone


class School(models.Model):
    """
    学校model，定义学校的相关属性
    """
    school_id = models.AutoField(
        max_length=20, primary_key=True, unique=True, verbose_name='school_id')  # 学校id
    school_name = models.CharField(
        max_length=50, null=True, blank=True, verbose_name='school_name')  # 学校名称
    school_code = models.CharField(
        max_length=20, null=True, blank=True, verbose_name='school_code')  # 学校代码
    school_nature = models.CharField(
        max_length=200, null=True, blank=True, verbose_name='school_nature')  # 学校性质
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'school'  # 内嵌类指定学校表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_school', u'查看学校'),
            ('add_school', u'添加学校'),
            ('delete_school', u'删除学校'),
            ('change_school', u'修改学校'),
        )


class College(models.Model):
    """
    学院model，定义了学院的相关属性
    """
    college_id = models.AutoField(max_length=20, primary_key=True, unique=True, verbose_name='college_id')  # 学院id
    college_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='college_name')  # 学院名称
    college_num = models.CharField(max_length=20, null=True, blank=True, verbose_name='college_num')  # 学院编号
    college_principal = models.ForeignKey(User,on_delete=models.CASCADE, default='')  # 责任人
    college_phone = models.CharField(max_length=15, null=True, blank=True, verbose_name='college_phone')  # 电话
    school_name = models.ForeignKey(School, on_delete=models.CASCADE, db_column='school_name',
                                    verbose_name='school_name')  # 学院所属学校名称
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'college'  # 内嵌类指定学院表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_college', u'查看学院'),
            ('add_college', u'添加学院'),
            ('delete_college', u'删除学院'),
            ('change_college', u'修改学院'),
        )


class Team(models.Model):
    """
    团队model，定义了团队的相关属性
    """
    team_id = models.AutoField(max_length=20, primary_key=True, unique=True, verbose_name='team_id')  # 团队id
    team_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='team_name')  # 团队名称
    team_principal = models.ForeignKey(User, on_delete=models.CASCADE, default='')  # 责任人
    team_phone = models.CharField(max_length=20, null=True, blank=True, verbose_name='team_phone')  # 电话
    college_name = models.ForeignKey(College, on_delete=models.CASCADE, db_column='college_name',
                                     verbose_name='college_name')  # 团队所属学院名称
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'team'  # 内嵌类指定团队表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_team', u'查看团队'),
            ('add_team', u'添加团队'),
            ('delete_team', u'删除团队'),
            ('change_team', u'修改团队'),
        )


class ResearchGroup(models.Model):
    """
    课题组model， 定义了课题组的属性
    """
    research_group_id = models.AutoField(max_length=20, primary_key=True, unique=True,
                                         verbose_name='research_group_id')  # 课题组id
    research_group_name = models.CharField(max_length=50, null=False, blank=True,
                                           verbose_name='research_group_name')  # 课题组名称
    research_group_principal = models.ForeignKey(User, related_name='principal4', on_delete=models.CASCADE, default='')  # 责任人
    research_group_participant = models.ManyToManyField(User, related_name='participant4', default='')  # 参与人，可多选
    team_name = models.ForeignKey(Team, on_delete=models.CASCADE, db_column='team_id',
                                  verbose_name='team_name')  # 课题组所属团队名称
    subject_name = models.ForeignKey(Subject, on_delete=models.CASCADE, db_column='subject_name',
                                     verbose_name='subject_name')  # 课题组所属课题名称
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'research_group'  # 内嵌类指定课题组表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_research_group', u'查看课题组'),
            ('add_research_group', u'添加课题组'),
            ('delete_research_group', u'删除课题组'),
            ('change_research_group', u'修改课题组'),
        )
